package com.wq.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sun.jndi.ldap.Ber;
import com.wq.common.BaseContext;
import com.wq.common.R;
import com.wq.entity.ShoppingCart;
import com.wq.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.security.sasl.SaslServer;
import java.util.List;

/**
 * @ description
 * @ author wuyunpeng
 * @ create 2022-07-24 11:49
 */
@RestController
@Slf4j
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    ShoppingCartService shoppingCartService;

    /**
     * 根据userId查询该用户的购物车信息
     *
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        final Long userID = BaseContext.getCurrentID();
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, userID);
        lqw.orderByDesc(ShoppingCart::getCreateTime);
        final List<ShoppingCart> shoppingCartList = shoppingCartService.list(lqw);
        return R.success(shoppingCartList);
    }

    /**
     * 添加购物车逻辑
     * 1）设置用户id
     * 2）判断是菜品还是套餐
     * 3）从数据库中唯一查找一个购物车项目
     * 4）如果有结果，修改数量即可
     * 5）如果没结果，添加一条结果到数据库中
     * 6）给页面返回正确结果
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {

        final Long userID = BaseContext.getCurrentID();
        final Long dishId = shoppingCart.getDishId();
        shoppingCart.setUserId(userID);
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(shoppingCart.getUserId() != null, ShoppingCart::getUserId, userID);
        if (dishId != null) {
            /**
             * 如果 dishID != null 说明添加的是菜品
             */
            lqw.eq(ShoppingCart::getDishId, dishId);
        } else {
            /**
             * 如果 dishID == null 说明添加的是套餐
             */
            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        final ShoppingCart result = shoppingCartService.getOne(lqw);

        /**
         * 如果商品已在购物车内，修改 number 字段即可
         */
        if (result != null) {
            final Integer number = result.getNumber();
            shoppingCart.setNumber(number + 1);
            result.setNumber(number + 1);
            shoppingCartService.updateById(result);
        } else {
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
        }
        return R.success(shoppingCart);
    }

    /**
     * 根据用户id从数据库中删除数据即可
     *
     * @return
     */

    @DeleteMapping("/clean")
    public R<String> clean() {
        final Long userID = BaseContext.getCurrentID();
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, userID);
        shoppingCartService.remove(lqw);
        return R.success("清空购物车成功");
    }

    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart) {
        final Long userID = BaseContext.getCurrentID();
        shoppingCart.setUserId(userID);
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, userID);
        final Long dishId = shoppingCart.getDishId();
        if (dishId != null) {
            lqw.eq(ShoppingCart::getDishId, dishId);
        } else {
            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        final ShoppingCart result = shoppingCartService.getOne(lqw);
        final Integer number = result.getNumber();
        if (number > 1) {
            shoppingCart.setNumber(number - 1);
            result.setNumber(number - 1);
            shoppingCartService.updateById(result);
        } else {
            shoppingCart.setNumber(0);
            shoppingCartService.remove(lqw);
        }
        return R.success(shoppingCart);
    }
}
